package com.randi.file.controller;

import com.randi.common.annotation.Anonymous;
import com.randi.common.annotation.LogAnnotation;
import com.randi.common.core.domain.Result;
import com.randi.common.enums.BusinessType;
import com.randi.common.enums.ResultCode;

import com.randi.common.utils.page.PageHelperUtils;
import com.randi.file.entity.SysFile;
import com.randi.file.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ziye
 */
@RestController
@RequestMapping("/file")
@Api(value = "file", tags = "文件管理")
public class SysFileController {

    @Resource
    private ISysFileService iSysFileService;

    /**
     * 获取文件信息列表
     *
     * @param sysFile SysFile
     * @return Result
     */
    @PostMapping
    @ApiOperation(value = "获取文件信息列表", notes = "")
    public Result selectSysFileList(@RequestBody SysFile sysFile) {
        PageHelperUtils.initPage(sysFile.getParams());
        List<SysFile> sysFiles = iSysFileService.selectSysFileList(sysFile);
        return Result.success().resPage(sysFiles);
    }

    /**
     * 恢复删除文件数据
     *
     * @param fileHash 文件哈希
     * @return Result
     */
    @GetMapping("/recover/{fileHash}")
    @ApiOperation(value = "恢复删除文件数据", tags = {})
    @LogAnnotation(module = "文件管理", value = "恢复文件删除数据", businessType = BusinessType.UPDATE)
    public Result recoverSysFileDeleted(@PathVariable String fileHash) {
        int status = iSysFileService.recoverSysFileDeleted(fileHash);
        if (status == 0) {
            return Result.error(ResultCode.FILE_UPLOAD_FAIL);
        }
        return Result.success(ResultCode.FILE_UPLOAD_SUCCESS);
    }

    /**
     * 删除文件信息
     *
     * @param fileId 文件Id
     * @return Result
     */
    @GetMapping("/delete/{fileId}")
    @ApiOperation(value = "删除文件信息")
    @LogAnnotation(module = "文件管理", value = "删除数据", businessType = BusinessType.DELETE)
    public Result deleteSysFile(@PathVariable Long fileId) {
        int status = iSysFileService.deleteSysFile(fileId);
        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_DELETE_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_DELETE_SUCCESS);
    }

    /**
     * 批量删除文件信息
     *
     * @param fileIds 文件Ids
     * @return Result
     */
    @PostMapping("/delete/batch")
    @ApiOperation(value = "删除文件信息")
    @LogAnnotation(module = "文件管理", value = "批量删除数据", businessType = BusinessType.DELETE)
    public Result deleteBatchSysFile(@RequestBody List<Long> fileIds) {
        int status = iSysFileService.deleteBatchSysFile(fileIds);
        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_DELETE_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_DELETE_SUCCESS);
    }

    /**
     * 获取文件下载地址
     *
     * @param fileId 文件ID
     * @return Result
     */
    @GetMapping("/download/create/{fileId}")
    @ApiOperation(value = "获取文件下载地址", tags = {})
    @LogAnnotation(module = "文件管理", value = "获取文件下载地址", businessType = BusinessType.DOWNLOAD)
    public Result createDownload(@PathVariable Long fileId) {
        String downloadUrl = iSysFileService.createDownloadUrl(fileId);
        if (downloadUrl == null) {
            return Result.error(ResultCode.FILE_DOWNLOAD_FAIL_CREATE);
        }
        return Result.success(ResultCode.FILE_DOWNLOAD_CREATE_SUCCESS).put(downloadUrl);
    }

    /**
     * 文件下载
     *
     * @param response response
     * @param taskId   任务ID
     */
    @Anonymous
    @GetMapping("/download/{taskId}")
    @ApiOperation(value = "文件下载", tags = {})
    public void downloadFile(HttpServletResponse response, @PathVariable String taskId) {
        iSysFileService.fileDownload(response, taskId);
    }

}
